﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace Y2_MineLand
{
    public partial class Xep_Hang : Form
    {
        List<string> Xep_hang = new List<string>();
        public Xep_Hang()
        {
            int i = 0;
            InitializeComponent();
            doc_file();
            label4.Text = Form1.time;
            while (i < Xep_hang.Count())
            {
                richTextBox1.Text += Xep_hang[i] + "\n";
                i++;
            }
        }

        public void doc_file()
        {
            StreamReader doc = File.OpenText(@"Xep_hang.txt");
            string input = null;
            while ((input = doc.ReadLine()) != null)
            {
                Xep_hang.Add(input);
            }
            doc.Close();

        }
        private int Valule(string xep_hang)
        {
            int i = xep_hang.IndexOf("_"); int ce = Convert.ToInt32(xep_hang.Remove(0, i + 1).Replace(" : ", "")); return ce;
        }
        private void sap_xep(List<string> Xep_hang)
        {
            string Tam = null;
            for (int i = 0; i < Xep_hang.Count-1; i++)
            {
                for (int j = i + 1; j < (Xep_hang.Count); j++)
                {
                    if (Valule(Xep_hang[j]) <= Valule(Xep_hang[i]))
                    {
                        Tam = Xep_hang[i];
                        Xep_hang[i] = Xep_hang[j];
                        Xep_hang[j] = Tam;
                    }
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            File.Delete(@"Xep_hang.txt");
            int i = 0;
            string tam = textBox1.Text + " _ " + Form1.time;
            Xep_hang.Add(tam);
            sap_xep(Xep_hang);
            if (Xep_hang.Count > 10)
                Xep_hang.RemoveRange(10, Xep_hang.Count - 10);
            System.IO.FileStream fs = new FileStream(@"Xep_hang.txt", System.IO.FileMode.Create);
            fs.Close();
            StreamWriter ghi = new StreamWriter(@"Xep_hang.txt");
            while (i < Xep_hang.Count)
            {
                ghi.WriteLine(Xep_hang[i]);
                i++;
            }
            ghi.Close();
            Close();
        }
    }
}
